package com.system.service.impl;

import com.alibaba.druid.sql.PagerUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.exception.BusinessException;
import com.common.exception.code.BaseResponseCode;
import com.common.utils.PageUtil;
import com.system.entity.RotationChart;
import com.system.mapper.RotationChartMapper;
import com.system.service.FileService;
import com.system.service.RotationChartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.vo.request.RotationChartDeleteReqVO;
import com.system.vo.request.RotationChartPageReqVo;
import com.system.vo.request.RotationChartReqAddVO;
import com.system.vo.request.RotationChartUpdateReqVO;
import com.system.vo.response.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 轮播图表结构 服务实现类
 * </p>
 *
 * @author XueHeng
 * @since 2020-09-17
 */
@Service
public class RotationChartServiceImpl extends ServiceImpl<RotationChartMapper, RotationChart> implements RotationChartService {

    @Autowired
    private FileService fileService;

    @Override
    public PageVO<RotationChart> getRotationCharts(RotationChartPageReqVo vo) {
        Page<RotationChart> page = new Page<>(vo.getPageNum(),vo.getPageSize());
        IPage<RotationChart> rotationCharts = this.baseMapper.selectAll(page, vo);
        return PageUtil.getPage(rotationCharts);
    }

    @Override
    public List<RotationChart> getRotationCharts() {
        List<RotationChart> rotationCharts = this.baseMapper.selectList(new QueryWrapper<>());
        return rotationCharts;
    }

    @Override
    @Transactional
    public void add(RotationChartReqAddVO vo, String userId) {
        RotationChart rotationChart = new RotationChart();
        rotationChart.setId(UUID.randomUUID().toString());
        BeanUtils.copyProperties(vo,rotationChart);
        rotationChart.setCreateId(userId);
        rotationChart.setCreateTime(new Date());
        this.save(rotationChart);
    }

    @Override
    @Transactional
    public void delete(List<RotationChartDeleteReqVO> vo) {
        List<String> ids = new ArrayList<>();
        List<String> fileUrl = new ArrayList<>();
        for(RotationChartDeleteReqVO v:vo){
            ids.add(v.getId());
            fileUrl.add(v.getFileUrl());
        }
        // 删除轮播图表中的数据
        this.removeByIds(ids);
        // 删除文件表上的数据和磁盘上的文件
        this.fileService.delete(fileUrl);
    }

    @Override
    @Transactional
    public void update(RotationChartUpdateReqVO vo, String userId) {
        RotationChart rotationChart = this.baseMapper.selectById(vo.getId());
        // 1、删除文件表上的数据和磁盘上的文件
        List<String> list = new ArrayList<>();
        list.add(rotationChart.getFileUrl());
        this.fileService.delete(list);
        // 2、更新轮播图数据
        if(rotationChart==null)
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        BeanUtils.copyProperties(vo,rotationChart);
        rotationChart.setUpdateId(userId);
        rotationChart.setUpdateTime(new Date());
        this.updateById(rotationChart);
    }
}
